package com.cindata

import grails.transaction.Transactional

@Transactional
class AbnormalMortgageBatchFlowStageService
{

    def initFlowStage(AbnormalMortgageBatch abnormalMortgageBatch, Workflow workflow)
    {
        def executionSequence = AbnormalMortgageBatchFlowStage.findByBatch(abnormalMortgageBatch, [sort: 'executionSequence', order: 'desc'])?.executionSequence
        def workflowStages = WorkflowStage.findAllByWorkflow(workflow)
        workflowStages?.each {
            def abnormalMortgageBatchFlowStage = new AbnormalMortgageBatchFlowStage()
            abnormalMortgageBatchFlowStage.executionSequence = executionSequence ? (executionSequence + it?.executionSequence) : it?.executionSequence
            abnormalMortgageBatchFlowStage.stage = it?.stage
            abnormalMortgageBatchFlowStage.canReject = it?.canReject
            abnormalMortgageBatchFlowStage.layout = it?.layout
            abnormalMortgageBatchFlowStage.batch = abnormalMortgageBatch
            if (abnormalMortgageBatchFlowStage.validate())
            {
                abnormalMortgageBatchFlowStage.save flush: true
            }
            else
            {
                println abnormalMortgageBatchFlowStage.errors
            }
        }
    }
}
